package com.yanqu.road.server.logic.activity.condition.snowgarden;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.fund.ActivityFundModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;

import java.math.BigInteger;
import java.util.List;

public class CommonActivityFundProgressCondition extends BaseActivityCondition {
    public CommonActivityFundProgressCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        ActivityFundModule module = player.getModule(ActivityFundModule.class);

        if (!checkBuyMallInfo()){
            return;
        }

        long progress = module.getProgress(activityInfo.getActivityId());
        if (progress == -1) {
            return;
        }

        setValue(BigInteger.valueOf(progress));
    }

    private boolean checkBuyMallInfo() {
        List<BigInteger> paramList = getActivityConditionInfo().getParamList();
        int mallKey = paramList.get(0).intValue();
        if (mallKey != 0) {
            // 付费
            MallInfo mallInfo = NormalActivityMgr.getRMBActivityMallInfoByGrade(activityConditionInfo.getActivityId(), mallKey);
            if (mallInfo == null) {
                return false;
            }
            return player.getModule(ActivityFundModule.class).isBuy(mallInfo);
        }
        return true;
    }

    @Override
    public boolean isCompleted(GamePlayer player) {
        List<BigInteger> paramList = getActivityConditionInfo().getParamList();
        if (!checkBuyMallInfo()) {
            // 付费
            return false;
        }
        if (paramList.get(3).compareTo(getValue()) <= 0) {
            return true;
        }
        return false;
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.CommonActivityFundProgress.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.CommonActivityFundProgress.getValue(), handle);
    }
}
